﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Runtime.Session;
using Camc.OutsourceProcession.Authorization.Users;
using Camc.OutsourceProcession.ManufacturingEquipment;
using Camc.OutsourceProcession.Organizations;
using Camc.OutsourceProcession.Printer.Dto;

namespace Camc.OutsourceProcession.Printer
{
	public class PrintEquipmentAppService: CrudAppService<PrintEquipment, PrintEquipmentDto, long, GetAllPrintEquipmentInput, CreatePrintEquipment, UpdatePrintEquipment>, IPrintEquipmentAppService
	{
		private readonly IOrganizationUnitAppService _organizationUnitAppService;
		private readonly IRepository<MyPrinter,long> _myPrinterRepository;
		public UserManager UserManager { get; set; }

		public PrintEquipmentAppService(IRepository<PrintEquipment, long> printEquipmentRepository, IOrganizationUnitAppService organizationUnitAppService, IRepository<MyPrinter, long> myPrinterRepository):base(printEquipmentRepository)
		{
			_organizationUnitAppService = organizationUnitAppService;
			_myPrinterRepository = myPrinterRepository;
		}

		public override PagedResultDto<PrintEquipmentDto> GetAll(GetAllPrintEquipmentInput input)
		{
			var result = base.GetAll(input);

			var allCompanys = _organizationUnitAppService.GetAllCompany().Result;

			var printerWithCompanyName = result.Items.Select(c =>
			{
				c.CompanyDisplayName = allCompanys.Find(d => d.Code == c.CompanyCode)?.DisplayName;
				return c;
			}).ToList();

			result.Items = printerWithCompanyName;

			return result;
		}

		public string GetMyPrinterInfo()
		{

			var user = UserManager.FindByIdAsync(AbpSession.GetUserId().ToString()).Result;

			if (user == null)
			{
				throw new Exception("There is no current user!");
			}

			var myPrinter = _myPrinterRepository.GetAll().Where(c=>c.UserName == user.UserName)?.FirstOrDefault();

			if (myPrinter ==null)
			{
				return "未设置默认打印机。";
			}
			else
			{
				var printer = Repository.Get(myPrinter.PrintEquipmentId);

				var myPrinterInfo = $"您绑定的打印机是{printer.DisplayName},SN{printer.SN}。";

				return myPrinterInfo;
			}
		}

		public string GetMyPrinterSN()
		{

			var user = UserManager.FindByIdAsync(AbpSession.GetUserId().ToString()).Result;

			if (user == null)
			{
				throw new Exception("There is no current user!");
			}

			var myPrinter = _myPrinterRepository.GetAll().Where(c => c.UserName == user.UserName)?.FirstOrDefault();

			if (myPrinter == null)
			{
				return "";
			}
			else
			{
				var printer = Repository.Get(myPrinter.PrintEquipmentId);

				var myPrinterInfo = $"{printer.SN}";

				return myPrinterInfo;
			}
		}

		public List<ComboboxItemDto> GetAllCanUsingPrinterForComboBox()
		{
			var comboboxItemDto = new List<ComboboxItemDto>();

			var allPrinter = this.GetAll(new GetAllPrintEquipmentInput
			{
				CompanyCode = "",
				DisplayName = "",
				MaxResultCount = 500,
				SkipCount = 0,
				SN = "",
				Sorting = ""
			});

			foreach (var printer in allPrinter.Items)
			{
				comboboxItemDto.Add(new ComboboxItemDto
				{
					Value = printer.Id.ToString(),
					DisplayText = printer.DisplayName
				});
			}

			return comboboxItemDto;
		}

		public void SaveMyPrinter(long printerId)
		{
			var user = UserManager.FindByIdAsync(AbpSession.GetUserId().ToString()).Result;

			if (user == null)
			{
				throw new Exception("There is no current user!");
			}

			var myPrinter = _myPrinterRepository.GetAll().Where(c => c.UserName == user.UserName)?.FirstOrDefault();
			
			if (myPrinter == null)
			{
				var newMyPrinter = new MyPrinter();
				newMyPrinter.UserName = user.UserName;
				newMyPrinter.PrintEquipmentId = printerId;

				_myPrinterRepository.Insert(newMyPrinter);
			}
			else
			{
				myPrinter.UserName = user.UserName;
				myPrinter.PrintEquipmentId = printerId;

				_myPrinterRepository.Update(myPrinter);
			}
		}

		

	}
}
